package com.itwk.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.Query;
import com.itwk.gulimall.product.dao.PmsCategoryBrandRelationDao;
import com.itwk.gulimall.product.entity.PmsBrandEntity;
import com.itwk.gulimall.product.entity.PmsCategoryBrandRelationEntity;
import com.itwk.gulimall.product.entity.PmsCategoryEntity;
import com.itwk.gulimall.product.service.PmsBrandService;
import com.itwk.gulimall.product.service.PmsCategoryBrandRelationService;
import com.itwk.gulimall.product.service.PmsCategoryService;
import com.itwk.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("pmsCategoryBrandRelationService")
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationDao, PmsCategoryBrandRelationEntity> implements PmsCategoryBrandRelationService {
    @Autowired
    private PmsCategoryService categoryService;
    @Autowired
    private PmsBrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsCategoryBrandRelationEntity> page = this.page(
                new Query<PmsCategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<PmsCategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(PmsCategoryBrandRelationEntity pmsCategoryBrandRelation) {
        PmsCategoryEntity category= categoryService.getById(pmsCategoryBrandRelation.getCatelogId());
        PmsBrandEntity brand = brandService.getById(pmsCategoryBrandRelation.getBrandId());
        pmsCategoryBrandRelation.setBrandName(brand.getName());
        pmsCategoryBrandRelation.setCatelogName(category.getName());
        baseMapper.insert(pmsCategoryBrandRelation);
    }

    @Override
    public  List<PmsCategoryBrandRelationEntity>  getRelation(Long brandId) {
        List<PmsCategoryBrandRelationEntity>  categoryBrandRelationEntity =
                baseMapper.selectList(new QueryWrapper<PmsCategoryBrandRelationEntity>().eq("brand_id", brandId));
        return categoryBrandRelationEntity;
    }

    @Override
    public void updateCategoryRelation(Long catId, String name) {
        baseMapper.updateCategoryRelation(catId,name) ;//在mapper中增加方法，测试xml 手写sql
    }

    @Override
    public List<BrandVo> getCategoryBrand(Long catId) {
        if(catId==null){
            return null;
        }
        //通过分类id查询到关联表中所有信息
        List<PmsCategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(new QueryWrapper<PmsCategoryBrandRelationEntity>().eq("catelog_id", catId));
        //遍历取出需要的品牌id和名称，此表有冗余字段，不要再次查询
        List<BrandVo> brandVos = categoryBrandRelationEntities.stream().map((enetity) -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(enetity, brandVo);
            return brandVo;
        }).collect(Collectors.toList());

        return brandVos;
    }

    @Override
    public void updateBrandRelation(Long brandId, String name) {
        baseMapper.upateBrandRelation(brandId,name);  //在mapper中增加方法，测试xml 手写sql
    }

}